package com.hjy.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class NameListController {

    @RequestMapping("/getNameList")
    public List<Object> getNameList() {
        ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("myExcel.xlsx"));

        List<List<Object>> read =
                reader.read();
        List<Object> collect = read.stream().map(objects -> {
            return objects.get(0);
        }).collect(Collectors.toList());
        return collect;
    }

    @PostMapping("/getNameListByFile")
    public List<Object> getNameListByFile(MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());

        List<List<Object>> read =
                reader.read();
        List<Object> collect = read.stream().map(objects -> {
            return objects.get(0);
        }).collect(Collectors.toList());
        return collect;
    }
}
